package foo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

public class Reg {

	@Test
	public void testts() {
		Pattern p = Pattern.compile(getStart());
		String text = "dddd<span></span><h1></h1><a>ss</a>hh<a>ss<a>ss</a></a>ssxx";
		Matcher m = p.matcher(text);
		if (m.find()) {
			System.out.println(text.substring(0, m.start()));
			for (int i = 0; i <= m.groupCount(); i++) {
				System.out.println(m.group(i) + "  " + i);
				if (i == m.groupCount()) {
					p = Pattern.compile(getEnd());
					out(p.matcher(m.group(i)), m.group(i));
				}
			}

		}
	}

	void out(Matcher m, String text) {
		if (m.find()) {
			System.out.println(text.substring(0, m.start()));
			for (int i = 0; i <= m.groupCount(); i++) {
				System.out.println(m.group(i) + "  " + i);
				if (i == m.groupCount()) {
					Pattern p = Pattern.compile(getEnd());
					out(p.matcher(m.group(i)), m.group(i));
				}
			}

		}
	}

	String getStart() {
		return "(<a>)|(<h1>)|(<span>)(.*)";
	}

	String getEnd() {
		return "(</a>)|(</h1>)|(</span>)(.*)";
	}
}
